<?php


namespace App\Command\Task;


use App\Utils\Helper\GetterHelper;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;

class DeployCommand extends Command {
    protected static $defaultName = 'task:deploy';

    protected function configure () {
        $this->setDescription('更新部署：队列重启、清空缓存、修正缓存文件夹权限');
    }

    protected function execute (InputInterface $input, OutputInterface $output) {
        $application = $this->getApplication();
        $section = $output->section();

        // 重启队列
        $table = new Table($section);
        $table->setHeaders(['任务1：重启队列']);
        $table->render();
        $command = $application->find('messenger:stop-workers');
        $arguments = [
            'command' => 'messenger:stop-workers',
        ];
        $greetInput = new ArrayInput($arguments);
        $bufferedOutput = new BufferedOutput();
        $command->run($greetInput, $bufferedOutput);
        $outputContent = trim($bufferedOutput->fetch(), PHP_EOL);
        $table->appendRow([$outputContent]);
        if (!preg_match('/successfully/i', $outputContent)) {
            $table->appendRow([PHP_EOL.'<error>[ERROR] An error has occurred.</error>']);
        }

        // 定时任务
        $table = new Table($section);
        $table->setHeaders(['任务2：同步定时任务']);
        $table->render();
        $command = $application->find('cronos:replace');
        $arguments = [
            'command'  => 'cronos:replace',
            '--server' => 'web',
        ];
        $greetInput = new ArrayInput($arguments);
        $bufferedOutput = new BufferedOutput();
        $command->run($greetInput, $bufferedOutput);
        $outputContent = trim($bufferedOutput->fetch(), PHP_EOL);
        $table->appendRow([$outputContent]);
        if (!preg_match('/successfully/i', $outputContent)) {
            $table->appendRow([PHP_EOL.'<error>[ERROR] An error has occurred.</error>']);
        }

        // 清空缓存
        $table = new Table($section);
        $table->setHeaders(['任务3：清空缓存']);
        $table->render();
        $command = $application->find('cache:clear');
        $arguments = [
            'command' => 'cache:clear',
        ];
        $greetInput = new ArrayInput($arguments);
        $bufferedOutput = new BufferedOutput();
        $command->run($greetInput, $bufferedOutput);
        $outputContent = trim($bufferedOutput->fetch(), PHP_EOL);
        $table->appendRow([$outputContent]);
        if (!preg_match('/successfully/i', $outputContent)) {
            $table->appendRow([PHP_EOL.'<error>[ERROR] An error has occurred.</error>']);
        }

        // 修正缓存文件夹权限
        $table = new Table($section);
        $table->setHeaders(['任务4：修正缓存文件夹权限']);
        $table->render();
        $cache_dir = GetterHelper::getParameter('kernel.cache_path');
        exec('chmod -R 777 '.$cache_dir);
        $table->appendRow([' [OK] successfully.']);
    }
}